package oj_leetcode;

public class AddBinary {
    public String addBinary(String a, String b) {
        boolean carry = false;
        int index_a = a.length() - 1;
        int index_b = b.length() - 1;
        String res = "";
        while ((index_a >= 0 && index_b >= 0) || carry) {
            char curr = '0';
            if (index_a >= 0) {
                if (a.charAt(index_a) == '1') {
                    curr += 1;
                }
                index_a--;
            }
            if (index_b >= 0) {
                if (b.charAt(index_b) == '1') {
                    curr += 1;
                }
                index_b--;
            }
            if (carry) {
                curr += 1;
            }
            if (curr >= '2') {
                carry = true;
                curr -= 2;
            } else {
                carry = false;
            }
            res = curr + res;
        }
        return res;
    }

    public static void main(String[] args) {
        new AddBinary().addBinary("1", "1");
    }
}
